<?php

function b_mydownloads_top_show($options) {
	global $xoopsDB;

       $mydirname = empty( $options[3] ) ? "mydownloads" : $options[3] ;
         if($options[0] == "date"){
       $this_template = empty( $options[4] ) ? 'db:'.$mydirname.'_block_new.html' : trim( $options[4] ) ;
       }
       else{
       $this_template = empty( $options[4] ) ? 'db:'.$mydirname.'_block_top.html' : trim( $options[4] ) ;
       }
       if( preg_match( '/[^0-9a-zA-Z_-]/' , $mydirname ) ) die( 'Invalid mydirname' ) ;

	$block = array();
    	//ver2.5
    	$block['mydownloads_weburl'] = XOOPS_URL . '/modules/' . $mydirname;
	$myts =& MyTextSanitizer::getInstance();
	//$order = date for most recent reviews
	//$order = hits for most popular reviews
	$result = $xoopsDB->query("SELECT lid, cid, title, date, hits FROM ".$xoopsDB->prefix($mydirname."_downloads")." WHERE status>0 ORDER BY ".$options[0]." DESC",$options[1],0);
	while($myrow=$xoopsDB->fetchArray($result)){
		$download = array();
		$title = $myts->makeTboxData4Show($myrow["title"]);
		if ( !XOOPS_USE_MULTIBYTES ) {
			if (strlen($myrow['title']) >= $options[2]) {
				$title = $myts->makeTboxData4Show(substr($myrow['title'],0,($options[2] -1)))."...";
			}
		}
		$download['id'] = $myrow['lid'];
		$download['cid'] = $myrow['cid'];
		$download['title'] = $title;
		if($options[0] == "date"){
			$download['date'] = formatTimestamp($myrow['date'],"s");
		}elseif($options[0] == "hits"){
			$download['hits'] = $myrow['hits'];
		}
		$block['downloads'][] = $download;
	}

	if( empty( $options['disable_renderer'] ) ) {
		require_once XOOPS_ROOT_PATH.'/class/template.php' ;
		$tpl =& new XoopsTpl() ;
		$tpl->assign( 'block' , $block ) ;
		$ret['content'] = $tpl->fetch( $this_template ) ;
		return $ret ;
	} else {
		return $block ;
	}

}

function b_mydownloads_top_edit($options) {

       $mydirname = empty( $options[3] ) ? "mydownloads" : $options[3] ;
         if($options[0] == "date"){
       $this_template = empty( $options[4] ) ? 'db:'.$mydirname.'_block_new.html' : trim( $options[4] ) ;
       }
       else{
       $this_template = empty( $options[4] ) ? 'db:'.$mydirname.'_block_top.html' : trim( $options[4] ) ;
       }
       if( preg_match( '/[^0-9a-zA-Z_-]/' , $mydirname ) ) die( 'Invalid mydirname' ) ;

	$form = ""._MB_MYDOWNLOADS_DISP."&nbsp;";
	$form .= "<input type=\"hidden\" name=\"options[]\" value=\"";
	if($options[0] == "date"){
		$form .= "date\"";
	}else {
		$form .= "hits\"";
	}
	$form .= " />";
	$form .= "<input type=\"text\" name=\"options[]\" value=\"".$options[1]."\" />&nbsp;"._MB_MYDOWNLOADS_FILES."";
	$form .= "&nbsp;<br />"._MB_MYDOWNLOADS_CHARS."&nbsp;<input type='text' name='options[]' value='".$options[2]."' />&nbsp;"._MB_MYDOWNLOADS_LENGTH."";
         $form .= "<input type='hidden' name='options[]' value='".$mydirname."' />";
         $form .= "<input type='hidden' name='options[]' value='".htmlspecialchars($this_template,ENT_QUOTES)."' />";
	return $form;
}

?>